package logic

import (
	"context"
	"encoding/json"
	"netease/models/mysql"
	"netease/models/redis"
	"time"

	"netease/neteaserpc/internal/svc"
	"netease/neteaserpc/neteaserpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListLogic {
	return &ListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表
func (l *ListLogic) List(in *neteaserpc.ListRequest) (*neteaserpc.ListResponse, error) {
	// todo: add your logic here and delete this line
	var netease []mysql.Netease
	Redisdata := redis.Rdb.Get(l.ctx, "list").Val()
	if Redisdata == "" {
		mysql.DB.Find(&netease)
		marshal, _ := json.Marshal(netease)
		redis.Rdb.Set(l.ctx, "list", marshal, time.Hour*1)
	} else {
		json.Unmarshal([]byte(Redisdata), &netease)
	}
	var lists []*neteaserpc.Lists
	for _, net := range netease {
		list := neteaserpc.Lists{
			Id:       int64(net.ID),
			Account:  net.Account,
			Password: net.Password,
			Content:  net.Content,
		}
		lists = append(lists, &list)
	}
	return &neteaserpc.ListResponse{
		List: lists,
	}, nil
}
